/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.android.auth.AuthUtils;
import com.example.android.R;
import com.google.api.services.plus.Plus;
import com.google.api.services.plus.model.Activity;

import java.io.IOException;
import java.util.List;

/**
 * Creates a tab for each Google+ activity
 *
 * @author Chirag Shah
 */
public class HomeActivity extends android.app.Activity {
  public static final String TAG = HomeActivity.class.getName();
  private ListView mListView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Intent intent = getIntent();
    if (intent == null || !intent.hasExtra("token")) {
      AuthUtils.refreshAuthToken(this);
      return;
    }

    setContentView(R.layout.activity_list);
    mListView = (ListView) findViewById(R.id.activityList);

    AsyncTask<String, Void, List<Activity>> task = new AsyncTask<String, Void, List<Activity>>() {
      @Override
      protected List<Activity> doInBackground(String... params) {
        try {
          Plus plus = new PlusWrap(HomeActivity.this).get();
          return plus.activities().list("me", "public").execute().getItems();
        } catch (IOException e) {
          Log.e(TAG, "Unable to list recommended people for user: " + params[0], e);
        }
        return null;
      }

      @Override
      protected void onPostExecute(List<Activity> feed) {
        if (feed != null) {
          mListView.setAdapter(new ActivityArrayAdapter(getApplicationContext(), feed));
        }
      }
    };
    task.execute("me");
  }
}
